//
//  WD_ShowImageViewController.m
//  BUDING
//
//  Created by 景强 on 16/4/12.
//  Copyright © 2016年 HanWoonYong. All rights reserved.
//

#import "WD_ShowImageViewController.h"
#import "WD_ShowImageCollectionViewCell.h"
#import "WD_TimeLineViewController.h"
#import "ImageUtil.h"
#import "ColorMatrix.h"
#import "WD_imageModel.h"

@interface WD_ShowImageViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UIActionSheetDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) UIImageView *filterView;
@property (nonatomic, strong) UILabel *filterLabel;
@property (nonatomic, assign) BOOL touch;
@property (nonatomic, strong) NSMutableDictionary *imgDic;
@property (nonatomic, strong) NSMutableArray *array;

@end

@implementation WD_ShowImageViewController

NSString *path;

-(NSMutableDictionary *)imgDic{
    if (_imgDic == nil) {
        _imgDic = [NSMutableDictionary dictionary];
    }
    return _imgDic;
}
- (UIImageView *)filterView {
    if (_filterView == nil) {
        _filterView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 50.0f, self.view.frame.size.width, 200.0f)];
        _filterView.userInteractionEnabled = YES;
        _filterView.contentMode = UIViewContentModeScaleAspectFit;
        
    }
    
    return _filterView;
    
}

- (UILabel *)filterLabel {
    if (_filterLabel == nil) {
        _filterLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 300.0f, self.view.frame.size.width, 25.0f)];
        _filterLabel.backgroundColor = [UIColor clearColor];
        _filterLabel.textColor = [UIColor orangeColor];
        _filterLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _filterLabel;
    
}







- (void)viewWillAppear:(BOOL)animated
{   [super viewDidAppear:animated];
    [self.collectionView setContentOffset:CGPointMake(self.numImage * self.view.frame.size.width, 0)];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.translucent = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    
    [self creatView];
}
// 滑动减速结束
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    self.numImage = self.collectionView.contentOffset.x / self.view.frame.size.width;
}




- (void)creatView {
    //    self.title = @"fuck";
    
    //    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(rightAction:)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"滤镜" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];;
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.pagingEnabled = YES;
    [self.collectionView registerClass:[WD_ShowImageCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:self.collectionView];
    
    
}

- (void)rightAction:(UIBarButtonItem *)sender {
    self.touch = !self.touch;
    
    //    if (self.touch) {
    //
    //        self.touch = YES;
    [self creatLvJing];
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"滤镜" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"原图",@"LOMO",@"黑白",@"复古",@"哥特",@"锐化",@"淡雅",@"酒红",@"清宁",@"浪漫",@"光晕",@"蓝调",@"梦幻",@"夜色", nil];
    [actionSheet showInView:self.view];
    //
    //    } else {
    //        self.touch = NO;
    
    
    
    
    
    
    //    }
    
    
}


- (void)setRelated:(WD_RelatedModel *)related {
    _related = related;
    _array = [NSMutableArray arrayWithCapacity:0];
    for (NSDictionary *dic in _related.images) {
        WD_imageModel *image = [[WD_imageModel alloc] init];
        [image setValuesForKeysWithDictionary:dic];
        [_array addObject:image];
    }
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WD_ShowImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    WD_imageModel *imageModel = self.array[indexPath.item];
    [cell.imageShow sd_setImageWithURL:[NSURL URLWithString:imageModel.url] placeholderImage:[UIImage imageNamed:@"占位图_"]];
    UIImage *img = cell.imageShow.image;
    [self.imgDic setObject:img forKey:[NSString stringWithFormat:@"%ld", indexPath.item]];
    NSData *data = UIImagePNGRepresentation(img);
    NSString *filePath = [NSString stringWithFormat:@"%@/%ld.png", path, indexPath.item];
    [data writeToFile:filePath atomically:YES];
    NSLog(@"😄%ld😄", self.numImage);
    cell.backgroundColor = [UIColor redColor];
    cell.userInteractionEnabled = YES;
    return cell;
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.array.count;
}



-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    WD_TimeLineViewController *time = [WD_TimeLineViewController new];
    [self.navigationController pushViewController:time animated:YES];
    
    
    //    [self.navigationController popToRootViewControllerAnimated:YES];
    
    
}



- (void)creatLvJing {
    // 判断手机版本
    if ([[[UIDevice currentDevice] systemVersion] floatValue] > 7.0f) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    //    self.filterView.image = [UIImage imageNamed:@"测试"];
    //
    //    [self.view addSubview:self.filterView];
    
    //    self.filterLabel.text = @"原图";
    //    [self.view addSubview:self.filterLabel];
    
    //    UIButton *sender = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [sender setTitle:@"" forState:UIControlStateNormal];
    //    sender.frame = CGRectMake(self.view.frame.size.width/2.0 - 50.0f, 350.0f, 100.0f, 30.0f);
    //    sender.backgroundColor = [UIColor redColor];
    //    sender.layer.cornerRadius = 5.0f;
    //    [sender addTarget:self action:@selector(senderAction) forControlEvents:UIControlEventTouchUpInside];
    //
    //    [self.view addSubview:sender];
    
    
    
    
    
    
}

- (void)senderAction
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"滤镜" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"原图",@"LOMO",@"黑白",@"复古",@"哥特",@"锐化",@"淡雅",@"酒红",@"清宁",@"浪漫",@"光晕",@"蓝调",@"梦幻",@"夜色", nil];
    [actionSheet showInView:self.view];
}

#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *str = @"";
    switch (buttonIndex) {
        case 0:
            str = @"原图";
            self.filterView.image = [UIImage imageNamed:@"测试"];
            [self huifuyuantu];
            break;
        case 1:
            str = @"LOMO";
            self.filterView.image = [ImageUtil imageWithImage:[UIImage imageNamed:@"测试"] withColorMatrix:colormatrix_lomo];
            [self changeImgColor:colormatrix_lomo];
            break;
        case 2:
            str = @"黑白";
            self.filterView.image = [ImageUtil imageWithImage:[UIImage imageNamed:@"测试"] withColorMatrix:colormatrix_heibai];
            [self changeImgColor:colormatrix_heibai];
            break;
        case 3:
            str = @"复古";
            self.filterView.image = [ImageUtil imageWithImage:[UIImage imageNamed:@"测试"] withColorMatrix:colormatrix_huajiu];
            [self changeImgColor:colormatrix_huajiu];
            break;
        case 4:
            str = @"哥特";
            self.filterView.image = [ImageUtil imageWithImage:[UIImage imageNamed:@"测试"] withColorMatrix:colormatrix_gete];
            [self changeImgColor:colormatrix_gete];
            break;
        case 5:
            str = @"锐化";
            self.filterView.image = [ImageUtil imageWithImage:[UIImage imageNamed:@"测试"] withColorMatrix:colormatrix_ruise];
            [self changeImgColor:colormatrix_ruise];
            break;
        case 6:
            str = @"淡雅";
            self.filterView.image = [ImageUtil imageWithImage:[UIImage imageNamed:@"测试"] withColorMatrix:colormatrix_danya];
            [self changeImgColor:colormatrix_danya];
            break;
        case 7:
            str = @"酒红";
            self.filterView.image = [ImageUtil imageWithImage:[UIImage imageNamed:@"测试"] withColorMatrix:colormatrix_jiuhong];
            [self changeImgColor:colormatrix_jiuhong];
            break;
        case 8:
            str = @"清宁";
            self.filterView.image = [ImageUtil imageWithImage:[UIImage imageNamed:@"测试"] withColorMatrix:colormatrix_qingning];
            [self changeImgColor:colormatrix_qingning];
            break;
        case 9:
            str = @"浪漫";
            self.filterView.image = [ImageUtil imageWithImage:[UIImage imageNamed:@"测试"] withColorMatrix:colormatrix_langman];
            [self changeImgColor:colormatrix_langman];
            break;
        case 10:
            str = @"光晕";
            self.filterView.image = [ImageUtil imageWithImage:[UIImage imageNamed:@"测试"] withColorMatrix:colormatrix_guangyun];
            [self changeImgColor:colormatrix_guangyun];
            break;
        case 11:
            str = @"蓝调";
            self.filterView.image = [ImageUtil imageWithImage:[UIImage imageNamed:@"测试"] withColorMatrix:colormatrix_landiao];
            [self changeImgColor:colormatrix_landiao];
            break;
        case 12:
            str = @"梦幻";
            self.filterView.image = [ImageUtil imageWithImage:[UIImage imageNamed:@"测试"] withColorMatrix:colormatrix_menghuan];
            [self changeImgColor:colormatrix_menghuan];
            break;
        case 13:
            str = @"夜色";
            self.filterView.image = [ImageUtil imageWithImage:[UIImage imageNamed:@"测试"] withColorMatrix:colormatrix_yese];
            [self changeImgColor:colormatrix_yese];
            break;
            
        default:
            break;
    }
    
    self.filterLabel.text = str;
}


- (void)changeImgColor:(const float *)colorMatrix{
    WD_ShowImageCollectionViewCell *cell = (WD_ShowImageCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:self.numImage inSection:0]];
    cell.imageShow.image = [ImageUtil imageWithImage:[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%ld.png", path, self.numImage]] withColorMatrix:colorMatrix];
}

- (void)huifuyuantu{
    WD_ShowImageCollectionViewCell *cell = (WD_ShowImageCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:self.numImage inSection:0]];
    cell.imageShow.image = [self.imgDic objectForKey:[NSString stringWithFormat:@"%ld", self.numImage]];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
